<?php

namespace app\admin\model;

use think\Model;

/**
 * ActivityTimes
 */
class SpecialActivityTimes extends Model
{
    // 表名
    protected $name = 'special_activity_times';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;
    protected $type = [
        'activity_times' => 'timestamp:Y-m-d H:i:s',
    ];

    public $append = ['activity_week_times','tickets_str'];

    public function getTicketsStrAttr($value,$row): array
    {
        if ($row['tickets'] === '' || $row['tickets'] === null) return [];
        if (!is_array($row['tickets'])) {
            $ticketsArr = explode(',', $row['tickets']);
            foreach ($ticketsArr as $key => $ticket) {
                if($ticket === '0') $ticketsArr[$key] = '免费';
                if($ticket > 0 ) $ticketsArr[$key] = $ticket . '元';
            }
            return $ticketsArr;
        }
    }

    /**
     * 活动时间格式化带星期
     */
    public function getActivityWeekTimesAttr($value,$row): string
    {
        return \ba\Date::formatWeekTime($row['activity_times']);
    }

    public function getRegistTypeAttr($value): array
    {
        if ($value === '' || $value === null) return [];
        if (!is_array($value)) {
            return explode(',', $value);
        }
        return $value;
    }

    public function setRegistTypeAttr($value): string
    {
        return is_array($value) ? implode(',', $value) : $value;
    }

    public function getActorIdentityAttr($value): array
    {
        if ($value === '' || $value === null) return [];
        if (!is_array($value)) {
            return explode(',', $value);
        }
        return $value;
    }

    public function setActorIdentityAttr($value): string
    {
        return is_array($value) ? implode(',', $value) : $value;
    }

    public function specialActivity(): \think\model\relation\BelongsTo
    {
        return $this->belongsTo(\app\admin\model\SpecialActivity::class, 'special_activity_id', 'id');
    }
}